//
//  FestivalTableViewCell2.m
//  代号：47
//
//  Created by 杨佩璋 on 15/7/1.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "FestivalTableViewCell2.h"
#import <UIImageView+WebCache.h>


@implementation FestivalTableViewCell2

-(void)dealloc{
    [_model release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, kScreen_width - 20, 40)];
        _titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _titleLabel.textColor = [UIColor orangeColor];
        CGFloat picWidth = (kScreen_width - 30)/3;
        CGFloat picHeight = picWidth / 1.5 + 30;

        _pic1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 60, picWidth, picHeight)];
        _pic2 = [[UIImageView alloc] initWithFrame:CGRectMake(10 + picWidth + 5, 60, picWidth, picHeight)];
        _pic3 = [[UIImageView alloc] initWithFrame:CGRectMake(10 + picWidth + 5 + picWidth + 5, 60, picWidth, picHeight)];
        
        _pic1.contentMode = UIViewContentModeScaleAspectFill ;
        _pic2.contentMode = UIViewContentModeScaleAspectFill;
        _pic3.contentMode = UIViewContentModeScaleAspectFill;
        _pic1.clipsToBounds = YES;
        _pic2.clipsToBounds = YES;
        _pic3.clipsToBounds = YES;
        _commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60 + picHeight +10, picWidth, 20)];
        _commentLabel.font = [UIFont systemFontOfSize:10];
        _commentLabel.textColor = [UIColor lightGrayColor];
        
        
        _publishTime = [[UILabel alloc] init];
        _publishTime.frame = CGRectMake(10, 60 + picHeight +10, kScreen_width - 30, 20);
        _publishTime.textAlignment = NSTextAlignmentRight;
        _publishTime.font = [UIFont systemFontOfSize:10];
        _publishTime.textColor = [UIColor lightGrayColor];
        
        [self.contentView addSubview:_pic3];
        [self.contentView addSubview:_pic1];
        [self.contentView addSubview:_pic2];
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_commentLabel];
        [self.contentView addSubview:_publishTime];
        
        
        [_titleLabel release];
        [_pic1 release];
        [_pic2 release];
        [_pic3 release];
        [_commentLabel release];
        
    }
    return self;
}


-(void)setModel:(FestivalModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    

    NSString *url1 = model.images[0][@"url2"];
    NSString *url2 = model.images[1][@"url2"];
    NSString *url3;
    
    if (model.images.count>2) {
        url3 = model.images[2][@"url2"];
        [_pic3 sd_setImageWithURL:[NSURL URLWithString:url3]];
    }

    

    [_pic1 sd_setImageWithURL:[NSURL URLWithString:url1]];
    [_pic2 sd_setImageWithURL:[NSURL URLWithString:url2]];
    
    NSString *url3;
    
    if (model.images.count>2) {
        url3 = model.images[2][@"url2"];
        [_pic3 sd_setImageWithURL:[NSURL URLWithString:url3]];
    }


    NSString *count = [NSString stringWithFormat:@"%@",model.commentCount];
    _commentLabel.text = [NSString stringWithFormat:@"评论数%@",count];
    _titleLabel.text = model.title;
    
    /**
     *  nslog
     */
  
   NSLog(@"%s %d %@",__FUNCTION__,__LINE__,model.publishTime);
    double time = [model.publishTime doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSDate *currentDate = [NSDate date];
    NSDate *newDate = [NSDate dateWithTimeInterval:8*60*60 sinceDate:currentDate];
    NSTimeInterval minus = [newDate timeIntervalSinceDate:date];
    /*分钟*/
    NSTimeInterval minute =  minus/60;
    /*小时*/
    NSTimeInterval hour = minute/60;
    NSString *timeStr;
    if (minute < 60) {
        timeStr = [NSString stringWithFormat:@"%.0f分钟前",minute];
    }else if (hour<24){
        timeStr = [NSString stringWithFormat:@"%.0f小时前",hour];
    }else{
        timeStr = [NSString stringByDate:date];
    }
    _publishTime.text = timeStr;
//    _publishTime.backgroundColor = [UIColor redColor];
    
}



@end
